#ifndef REMOTE_HANDLE_H
#define REMOTE_HANDLE_H

#include <wx/event.h>
#include <string>
#include <condition_variable>
#include <mutex>
#include <atomic>
#include <fstream>
#include <oatpp-websocket/WebSocket.hpp>
#include "PixelFormat.h"

class RemoteCommandAndParament: public wxEvent
{
public:
    RemoteCommandAndParament(wxEventType eventType, int winid,int32_t Parament)
        : wxEvent(winid, eventType),
        parament(Parament)
    {}
    int32_t GetParament() {return parament;}
    virtual wxEvent *Clone() const { return new RemoteCommandAndParament(*this); }
private:
    int32_t parament;
};

wxDECLARE_EVENT(EVT_GETDEVICESTATUS,wxCommandEvent);
wxDECLARE_EVENT(EVT_COMMAND_INITDEVICE,wxCommandEvent);
wxDECLARE_EVENT(EVT_COMMAND_STARTSHOT,wxCommandEvent);
wxDECLARE_EVENT(EVT_COMMAND_STARTSAVE,wxCommandEvent);
wxDECLARE_EVENT(EVT_COMMAND_STOPSHOT,wxCommandEvent);
wxDECLARE_EVENT(EVT_COMMAND_STOPSAVE,wxCommandEvent);
wxDECLARE_EVENT(EVT_COMMAND_GETCAMERAPARA,wxCommandEvent);
wxDECLARE_EVENT(EVT_COMMAND_ENABLE_TRIGGER,wxCommandEvent);
wxDECLARE_EVENT(EVT_COMMAND_DISABLE_TRIGGER,wxCommandEvent);
wxDECLARE_EVENT(EVT_COMMAND_FILETRANSFER_ENABLE,wxCommandEvent);
wxDECLARE_EVENT(EVT_COMMAND_FILETRANSFER_DISABLE,wxCommandEvent);
wxDECLARE_EVENT(EVT_COMMAND_FILETRANSFER_REQUEST,wxCommandEvent);
//带参数的事件
wxDECLARE_EVENT(EVT_COMMAND_SET_EXPOSETIME,RemoteCommandAndParament);
wxDECLARE_EVENT(EVT_COMMAND_SET_IMAGEHEIGHT,RemoteCommandAndParament);
wxDECLARE_EVENT(EVT_COMMAND_SET_IMAGEWIDTH,RemoteCommandAndParament);
wxDECLARE_EVENT(EVT_COMMAND_SET_IMAGEOFFSETX,RemoteCommandAndParament);
wxDECLARE_EVENT(EVT_COMMAND_SET_IMAGEOFFSETY,RemoteCommandAndParament);
wxDECLARE_EVENT(EVT_COMMAND_SET_FRAMERATE,RemoteCommandAndParament);

struct CameraParament
{
    int32_t ImageHeight;
    int32_t ImageWidth;
    int32_t ImageOffsetX;
    int32_t ImageOffsetY;
    int32_t ExposeTime;
    int32_t FrameRate;
    int32_t TriggerMode;
};

//用于远程控制相机，提供到GUI的接入服务
class RemoteHandle
{
    public:
        RemoteHandle(wxEvtHandler* evtHandler);
        virtual ~RemoteHandle();
        void SetCameraContrlHandler(wxEvtHandler* evtHandler);
        void StartSocket();
        void SetImage(unsigned char* image,int32_t height,int32_t width,pixelFormat imgFormat);
        void SendDeviceStatus(int32_t value);
        void SendCameraPara(CameraParament cameraParament);
        void AcceptData(const char* aFrame,long size); //用于接收数据，线程安全设计
        std::string SendData();  //如果没有发送任务，这个函数会让当前线程休眠
        void SetSocket(std::shared_ptr<oatpp::websocket::WebSocket>& socket);
        void closeTask();
        void SendMessage(const std::string& msg);
        void FileTransfer(const std::string& filePath);
        void StopFileTransfer();
        void SendFileData(const std::shared_ptr<oatpp::websocket::WebSocket>& socket);  //文件数据传输调用,一直调用到传输结束为止，这样可以提高性能
        bool FileTransferFuncEnable();
    private:
        wxEvtHandler* _evtHandler;
        wxEvtHandler* _cameraContralHandler=nullptr;
        std::condition_variable condSendReady;  //数据准备就绪后唤醒线程
        std::mutex sendDataMut;
        std::mutex sendImageMut;
        std::mutex sendCommandMut;
        std::string sendData;
        std::string sendCommand;
        std::atomic<bool> sendImageFlag=false;
        std::atomic<bool> sendDataFlag=false;
        std::atomic<bool> sendCommandFlag=false;
        std::atomic<bool> threadExitFlag=false;
        std::shared_ptr<oatpp::websocket::WebSocket> socketPtr;

        //文件传输
        void SendFileTransferCommand(const int32_t& value);
        void SendFileTransferCommand(const char* value,size_t size);
        void GetFileTransferCommand();
        long allFrameCount=0;
        std::ifstream iFile;
        std::atomic<bool> fileDataReady=false;
        std::mutex FileDataMutex;
        std::condition_variable condFileDataReady;
        std::atomic<bool> fileTransferFlag=false;
        bool fileTransferEnable=false;
};

#endif
